// pages/location/location.js
import { getLocation,mapdirection } from "../../utils/map.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    latitude:"",
    longitude:"",
    showMap:false,
    polyline:[],
    markers:[],
    distance:"",
    duration:"",
    addressdec:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    // console.log(options);
    let parm = JSON.parse(options.parm)
    console.log(parm);
    const direction = {
      latitude:parm.location.lat,
      longitude:parm.location.lng
    }
    this.setData({
      addressdec:{
        address:parm.address,
        title:parm.title
      }
    })
    this.getLocation(direction)
  },
  getLocation(direction){
    getLocation().then((res) => {
      console.log(res);
      let { latitude,longitude } = res
      this.setData({
        latitude,
        longitude,
        showMap:true
      })
      this.setData({
        markers:[
          {
            id:0,
            iconPath:'../../images/mapMarker.png',
            latitude,
            longitude,
            width:28,
            height:28,
            zIndex:3,
            anchor: {
              x: 0.5,
              y: 1
            }
          },
          {
            id:1,
            iconPath:'../../images/mapMarker.png',
            latitude:direction.latitude,
            longitude:direction.longitude,
            width:28,
            height:28,
            zIndex:3,
            anchor: {
              x: 0.5,
              y: 1
            }
          }
        ]
      })
      mapdirection({
        latitude,
        longitude
      },
        direction
      ).then((res) => {
        console.log(res);
        const coors = res.result.routes[0].polyline , pl = []
        //坐标解压（返回的点串坐标，通过前向差分进行压缩）
        const kr = 1000000
        for(let i = 2; i < coors.length; i++){
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr
        }
         //将解压后的坐标放入点串数组pl中
        for (var i = 0; i < coors.length; i += 2) {
          pl.push({ latitude: coors[i], longitude: coors[i + 1] })
        }
        console.log(pl);
        this.setData({
          latitude:pl[0].latitude,
          longitude:pl[0].longitude,
          polyline:[{
            points:pl,
            color:"#0091ff",
            width:6
          }],
          distance:res.result.routes[0].distance,
          duration:res.result.routes[0].duration
          
        })
      }).catch((err) => {
        console.log(err);
      })
    })  
  },
  toMapApp(){
    wx.showActionSheet({
      itemList: ['高德地图/百度地图'],
      success:((res) => {
        console.log(res);
        if(!res.tapIndex){
          // console.log(1);
          wx.openLocation({
            latitude: this.data.latitude,
            longitude: this.data.longitude,
            name:this.data.addressdec.title,
            address:this.data.addressdec.address,
            scale:28
          })
        }
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})